        TITL DAI FIRMWARE 3E000-3E1F5  V1.1
        ORG   :E000
*
*
*
*   ===========================
*** ENCODING / UTILITY PACKAGES ***
*   ===========================
*
* Called by RST 1; DATA XX. XX indicates the
* offset of E000 for the different entrypoints.
*
* Contains also the key encoding routines and the
* Heap organisation routines.
*
***************
* ENTRYPOINTS *
***************
*
ELINE   JMP   L3E2      Encode a BASIC line
VELN    JMP   ELN       Encode a linenr
ETCON   JMP   L3E18     Encode a constant
USTART  JMP   RESET     Start Utility package
DBOOT   JMP   L3E353    Disc bootstrap
MHREQ   JMP   HREQ      Heap request
MINKEY  JMP   INKEY     Encode a key input
L3E394  JMP   L3E143    Get inputs from keyb
 REM or DINC
*
*   ================
*** ENCODING PACKAGE ***
*   ================
*
* Generally in the encoding routines, HL points to
* the first free location in the EBUF. C points to
* the input text line.
*
***********************
* UPDATE EBUF POINTER *
***********************
*
* Increments the input pointer and checks if the
* encoded input buffer (EBUF) is full.
*
* Entry: HL: input pointer EBUF.
* Exit:  HL: updated pointer.
*        Other registers preserved.
*
INXCH   INX   H         Incr pointer
        PUSH  PSW
        MOV   A,L       Get lobyte in A
        CPI   :BE       Max value reached?
        MVI   A,:1A     Buffer full ?
        JNC   ERROR     Then run error 'LINE TOO
 REM COMPLEX'
        POP   PSW
        RET 
*
**************************
* ENCODE A BASIC COMMAND *
**************************
*
* Looks for a match between input on line and the
* BASIC command table #CBBF. Error exit 'COMMAND
* INVALID' if the 2 high order bits of the code in
* the table don't match the mask in D. Else the
* code (#80 + low order 6 bits from code in table)
* are stored in EBUF.
*
* Entry: HL: 1st free position in EBUF.
*        D : Mask for direct command (#80) or
*            program input (#40).
*        C : Position on current line.
* Exit:  HL: Updated.
*        C : Points to 1st not used input in line.
*        E : Code just stored.
*        BD preserved, AF corrupted.
*        Exit with jump to address found in table.
*        #E04F is on stack as next returnaddress,
*        entry DE is next on stack.
*
L3E2    PUSH  D
        PUSH  H
        LXI   H,L3E3    Returnaddr after finishing
 REM encoding
        XTHL            Save it on stack
        PUSH  H         Save HL again
        MVI   E,:02     Addit. offset for finding
 REM instruction in table
        LXI   H,SNEW    Startaddr table with strings
 REM Basiccommands
        PUSH  D
        CALL  LOOKC     Find instr in table
        POP   D         Get mask for direct/program
        MOV   A,M       Get instr code from table
        ANI   :C0       ) Check if it
        ANA   D         ) is a valid command
        MVI   A,:18     If not: Run error
        JZ    ERROR     'COMMAND INVALID'
        MOV   A,M       Get instr code from table
        INX   H
        ANI   :3F       ) Make it a token
        ORI   :80       )
        MOV   E,A       Store token in E
        MOV   A,M       )
        INX   H         ) Get addr of encoding
        MOV   H,M       ) instruction in HL
        MOV   L,A       )
        XTHL            and save it on stack
        MOV   M,E       Store token in EBUF
        CALL  INXCH     Update EBUF pointer
        RET             Go to encoding instruction
 REM return afterwards to E04F
*
* End of encoding of an input line (after running
* the command specific processing).
* Checks if character after BASIC command is CR or
* ';'.
*
* Entry: C : points to position on current line.
*        On stack: Original DE.
*
L3E3    POP   D         Get type of command in D
        CALL  IGNB      Get char from line, neglect
 REM tab + space
        INR   C         Pntr to next char
        CPI   :3A       ':' ?
        JZ    L3E2      Then encode next instr
        CPI   :0D       'CR' ?
        JNZ   ERRSN     Run 'SYNTAX ERROR' if not
        RET 
*
******************************
* ENCODE 'FOR - TO - (STEP)' *
******************************
*
* Valid for all specific encoding routines:
* HL points to 1st free EBUF location.
*
EFOR    CALL  ELET      Encode 'LET'
        CPI   :20       String type ?
        JZ    ERRTM     Then run error 'TYPE
 REM MISMATCH'
        CPI   :10       INT type ?
        LXI   D,L3E395  Addr 'TO' table
        JMP   L3E7      Get addr encoding instr and
 REM go to it
 REM
* Encode 'TO':
 REM
L3E5    CZ    L3E371    Encode a INT expr
        CNZ   L3E372    Encode a FPT expr
        LXI   D,L3E396  Addr 'STEP' table
        JMP   L3E7      Get addr encoding instr and
 REM go to it
 REM
* Encode 'STEP':
 REM
L3E6    CZ    L3E371    Encode a INT expr
        CNZ   L3E372    Encode a FPT expr
        RET 
 REM
* End encoding:
 REM
L3E363  MVI   M,:FF     FF in EBUF as separator
        CALL  INXCH     Update EBUF pointer
        RET 
 REM
* Table:
 REM
L3E395  DATA  :02
        DATA  :54       T
        DATA  :4F       O
        DBL   L3E5      Addr encode 'TO'
*
        DATA  :00       'TO' not found:
        DBL   ERRSN     Run 'SYNTAX ERROR'
*
L3E396  DATA  :04
        DATA  :53       S
        DATA  :54       T
        DATA  :45       E
        DATA  :50       P
        DBL   L3E6      Addr encode 'STEP'
*
        DATA  :00       'STEP' not found:
        DBL   L3E363    End command
*
*************************************************
* GET ADDRESS ENCODING INSTRUCTION AND GO TO IT *
*************************************************
*
* Entry: DE : Points to table of format:
*             < length name / name / jumpaddr > or
*             < 00 / jumpaddr >.
*        C  : Points to input.
* Exit:  C  : Updated.
*        AFBHL preserved. D=0, E=1.
*
L3E7    PUSH  H
        PUSH  PSW
        XCHG            Addr table in HL
        MVI   E,:01
        CALL  LOOKC     Find instruction in table.
 REM On exit, HL points to addr
 REM of encoding routine
        MOV   A,M       )
        INX   H         ) Get addr encoding instr
        MOV   H,M       ) in HL
        MOV   L,A       )
        POP   PSW
        XTHL            Addr encoding instr on stack
        RET             Go to it
*
***************************************
* ENCODE 'NEXT' AND 'NEXT <VARIABLE>' *
***************************************
*
ENEXT   CALL  TSEOC     Next char ':' or 'CR' ?
        RZ              Then ready
 REM
* If NEXT <variable>:
 REM
        DCX   H
        INR   M         Token +1 (#AC)
        INX   H
        CALL  L3E85     Encode variable or array ref
        LDA   TYPE      Get type latest expression
        CPI   :20       String type ?
        JZ    ERRTM     Then run error 'TYPE
 REM MISMATCH'
        RET 
*
**************************************
* ENCODE 'IF - THEN' AND 'IF - GOTO' *
**************************************
*
EIF     PUSH  H
        PUSH  D
        CALL  L3E376    Encode boolean expr (type
 REM #30)
        LXI   D,L3E397  Addr 'THEN' table
        JMP   L3E7      Get addr encoding instr
 REM and go to it
 REM
* Encode 'THEN':
 REM
L3E10   CALL  L3E114    Read a linenr into EBUF
        POP   D
        JNC   L3E11     Jump if no linenr given
 REM
* If linenr:
 REM
        XTHL
        DCX   H
        INR   M
        INR   M         Token +2 (#A8)
        POP   H
        RET 
 REM
* If statement:
 REM
L3E11   POP   PSW
        PUSH  H         Preserve EBUF pntr
        CALL  INXCH     Update EBUF pointer
        CALL  L3E2      Encode BASIC command
        MOV   A,L       Lobyte new EBUF pntr in A
        XTHL            Old EBUF pntr in HL
        SUB   L
        DCR   A
        MOV   M,A       Length string in EBUF entry
        POP   H
        DCR   C
        RET 
 REM
* Encode 'GOTO':
 REM
L3E12   POP   PSW
        XTHL
        DCX   H
        INR   M         Token +1 (#A7)
        POP   H
        CALL  ELN       Get linenr
        RET 
 REM
* Table:
 REM
L3E397  DATA  :04
        DATA  :54       T
        DATA  :48       H
        DATA  :45       E
        DATA  :4E       N
        DBL   L3E10     Addr encode 'THEN'
*
        DATA  :04
        DATA  :47       G
        DATA  :4F       O
        DATA  :54       T
        DATA  :4F       O
        DBL   L3E12     Addr encode 'GOTO'
*
        DATA  :00       If no 'THEN' or 'GOTO' found
        DBL   ERRSN     Run 'SYNTAX ERROR'
*
****************
* ENCODE 'LET' *
****************
*
* Encodes a variable or array with arguments. Then
* finds '=' in input (error if not). Then encodes
* an expression, depending on variable type (error
* if type non-existing).
*
* Exit: DE: Offset of left-side variable.
*       A and B: Left-side type.
*       C, HL updated. F corrupted.
*
ELET    CALL  L3E85     Encode var or array ref
        CALL  ECHRI     Check next char is '='
        DATA  :3D
        LDA   TYPE      Get type latest expression
        CPI   :00       FPT ?
        JZ    L3E372    Then encode FPT expr
        CPI   :10       INT ?
        JZ    L3E371    Then encode INT expr
        JMP   L3E377    Else encode STR expr
*
***************************************
* ENCODE 'INPUT' AND 'INPUT <STRING>' *
***************************************
*
EINPUT  CALL  IGNB      Get char from line, neglect
 REM tab + space
        CPI   :22       '"' ?
        JNZ   EREAD     Encode 'READ' if no string
 REM in INPUT statement
 REM
* If 'INPUT <string>':
 REM
        DCX   H
        INR   M         Token +1 (#A1)
        INX   H
        CALL  L3E377    Encode STR expr
        CALL  ECHRI     Check if next char is ';'
        DATA  :3B
 REM Into EREAD
*
*****************
* ENCODE 'READ' *
*****************
*
* From L3E16 used by several routines, with another
* address in LXI D, ...., pointing to various kinds
* of encoding/get routines.
*
EREAD   LXI   D,L3E85   Addr routine encode variable
 REM or array reference
*
L3E16   PUSH  H
        MVI   M,:00     00 into EBUF (count)
        CALL  INXCH     Update EBUF pointer
L3E17   PUSH  D
        CALL  L3E21     Go to encoding routine
 REM
* Return here after encoding:
 REM
        POP   D
        XTHL
        INR   M         Count in EBUF +1
        XTHL
        CALL  IGNB      Get char from line, neglect
 REM tab + space
        INR   C         Points to next char on line
        CPI   :2C       ',' ?
        JZ    L3E17     Again if more items
        DCR   C         Correct line pointer
        INX   SP
        INX   SP        SP to returnaddr
        RET 
*
**************************************
* ENCODE A NUMBER OR STRING CONSTANT *
**************************************
*
* Entry: A: Type.
*
L3E18   PUSH  PSW       Preserve type
        ORA   A
        JZ    L3E20     Jump if FPT type
        CPI   :10
        JZ    L3E137    Jump if INT type
 REM
* If STR type:
 REM
        CALL  IGNB      Get char from line, neglect
 REM tab + space
        CPI   :22       '"' (quoted string) ?
        PUSH  PSW
        CZ    L3E135    Then store quoted string
 REM in EBUF
        POP   PSW
        CNZ   L3E97     Else store unquoted string
 REM in EBUF
L3E19   POP   PSW
        RET 
 REM
* If FPT type:
 REM
L3E20   CALL  ENM25     Encode FPT nr into EBUF
        JMP   L3E139    Quit with evt 'SYNTAX ERROR'
*
*************************
* part of EREAD (3E12A) *
*************************
*
L3E21   PUSH  D         Addr encoding routine on
 REM stack
        RET             Go to it
*
****************
* ENCODE 'DIM' *
****************
*
EDIM    LXI   D,L3E23   Addr routine 'encoding array
 REM reference'
        JMP   L3E16     Continu encoding
*
*****************************
* ENCODE AN ARRAY REFERENCE *
*****************************
*
L3E23   CALL  L3E85     Encode var or array ref
        MOV   A,B       Type in A
        ANI   :40       Array type ?
        JZ    ERRBS     Run 'SUBSCRIPT ERROR' if not
        RET 
*
***************************************
* ENCODE 'ON - GOTO' AND 'ON - GOSUB' *
***************************************
*
EON     PUSH  H
        CALL  L3E371    Encode INT expr
        LXI   D,L3E399  Addr table
        JMP   L3E7      Get addr encoding instr
 REM and go to it
 REM
* Encode linenr in GOSUB (L3E25) and GOTO (L3E411):
 REM
L3E25   XTHL
        DCX   H
        INR   M         Token +1 (#AF)
        INX   H
        XTHL
L3E411  XTHL
        POP   H
        LXI   D,ELN     Addr routine 'get linenr'
        JMP   L3E16     Continu encoding
 REM
* Table:
 REM
L3E399  DATA  :04
        DATA  :47       G
        DATA  :4F       O
        DATA  :54       T
        DATA  :4F       O
        DBL   L3E411    Addr encode 'GOTO'
*
        DATA  :05
        DATA  :47       G
        DATA  :4F       O
        DATA  :53       S
        DATA  :55       U
        DATA  :42       B
        DBL   L3E25     Addr encode 'GOSUB'
*
        DATA  :00       If no GOTO or GOSUB found:
        DBL   ERRSN     Run 'SYNTAX ERROR'
*
******************
* ENCODE 'PRINT' *
******************
*
EPRINT  PUSH  H
        MVI   M,:00     00 into EBUF (init length)
        CALL  TSEOC     Next char ':' or 'CR' ?
        JZ    L3E29     Then ready
 REM
* If statement after PRINT:
 REM
L3E27   CALL  INXCH     Update EBUF pointer
        XTHL
        INR   M         Length +1
        XTHL
        CALL  EEXPI     Encode non-boolean expr
 REM preceeded by its type
        MVI   M,:FF     FF into EBUF
        CALL  TSEOC     Next char ':' or 'CR' ?
        JZ    L3E29     Then ready
 REM
* If more statements:
 REM
        MOV   M,A       Char into EBUF
        CPI   :2C       ',' ?
        JZ    L3E28     Then continu
        CPI   :3B       ';' ?
        JNZ   ERRSN     Run 'SYNTAX ERROR' if not
L3E28   INR   C         Update line pntr
        CALL  TSEOC     Next char ':' or 'CR' ?
        JNZ   L3E27     Continu if not
*
L3E29   XTHL
        POP   H
        CALL  INXCH     Update EBUF pointer
        RET 
*
*****************
* ENCODE 'MODE' *
*****************
*
EMODE   MVI   D,:FF     Default mode 0
        CALL  IGNB      Get char from line, neglect
 REM tab + space
        INR   C         Points to next char
        SUI   :30
        JZ    L3E31     If char is '0': FF in EBUF
        JC    ERRSN     Run 'SYNTAX ERROR' if not
 REM number or printable char
        CPI   :07       Between 0 and 7?
        JNC   ERRRA     Run error 'NUMBER OUT OF
 REM RANGE' if not
        DCR   A         )
        ADD   A         ) Calc code for mode
        MOV   D,A       in D
        CALL  EFETCH    Get next char from line
        CPI   :41       'A' ?
        JNZ   L3E31     Jump if not
        INR   C         Points to next char
        INR   D         Set D for A-mode
L3E31   MOV   M,D       Mode code in EBUF
        CALL  INXCH     Update EBUF pointer
        RET 
*
*
*
        UNL
ELN     EQU   :E72A
RESET   EQU   :EA74
L3E353  EQU   :EF90
HREQ    EQU   :E99C
INKEY   EQU   :E93F
L3E143  EQU   :E935
ERROR   EQU   :D9F5
SNEW    EQU   :CBBF
LOOKC   EQU   :CA34
IGNB    EQU   :DDD2
ERRSN   EQU   :DA0B
ERRTM   EQU   :DA1A
L3E371  EQU   :E36D
L3E372  EQU   :E376
TSEOC   EQU   :E859
L3E85   EQU   :E5BC
TYPE    EQU   :0136
L3E376  EQU   :E39C
L3E114  EQU   :E731
ECHRI   EQU   :E867
L3E377  EQU   :E3A1
L3E137  EQU   :E888
L3E135  EQU   :E880
L3E97   EQU   :E6A6
ENM25   EQU   :E501
L3E139  EQU   :E893
ERRBS   EQU   :DA29
EEXPI   EQU   :E3B2
ERRRA   EQU   :DA15
EFETCH  EQU   :DDE0
        LST
        END 
